8  All fish data

Note: need to do a full book render to get OJS functionality.

8.1 Merge All data

8.1.1 Read merged data target

Variables in the all data dataset

tibble [516,898 × 88] (S3: tbl_df/tbl/data.frame)
 $ tag                                      : chr [1:516898] "00088cbed0" "00088cbed3" "00088cbed4" "00088cbed4" ...
 $ detectionDate                            : POSIXct[1:516898], format: "2013-03-25 04:00:00" "2012-06-07 13:03:00" ...
 $ sampleName                               : chr [1:516898] "84" "81" "84" "85" ...
 $ sampleNumber                             : num [1:516898] 73 70 73 74 70 71 73 79 80 71 ...
 $ river                                    : chr [1:516898] "wb obear" "west brook" "west brook" "west brook" ...
 $ section                                  : num [1:516898] 7 1 40 44 25 32 32 3 3 23 ...
 $ area                                     : chr [1:516898] "trib" "inside" "inside" "inside" ...
 $ observedLength                           : num [1:516898] 62 62 109 154 113 120 147 70 74 86 ...
 $ survey                                   : chr [1:516898] "shock" "shock" "shock" "shock" ...
 $ pass                                     : num [1:516898] 1 1 1 1 1 1 1 1 1 1 ...
 $ observedWeight                           : num [1:516898] 1.9 2.7 12.6 41.3 15.5 18.3 30.4 3.5 4.8 6.7 ...
 $ comments                                 : chr [1:516898] "additional genetic sample" NA NA NA ...
 $ cohort                                   : num [1:516898] 2012 2012 2012 2012 2011 ...
 $ species                                  : chr [1:516898] "bkt" "bkt" "bkt" "bkt" ...
 $ dateEmigrated                            : Date[1:516898], format: NA NA ...
 $ sex                                      : chr [1:516898] NA NA NA NA ...
 $ year                                     : num [1:516898] 2013 2012 2013 2013 2012 ...
 $ season                                   : num [1:516898] 1 2 1 2 2 3 1 3 4 3 ...
 $ proportionSampled                        : num [1:516898] 1 1 1 1 1 1 1 1 1 1 ...
 $ lagDetectionDate                         : POSIXct[1:516898], format: NA NA ...
 $ meanTemperature                          : num [1:516898] NaN NaN 10.7 NaN 16.5 ...
 $ meanFlow                                 : num [1:516898] NaN NaN 0.5247 NaN 0.0338 ...
 $ meanFlowByRiver                          : num [1:516898] NaN NaN 0.598 NaN 0.135 ...
 $ meanFlowByArea_flowExt                   : num [1:516898] NaN NaN 0.5247 NaN 0.0338 ...
 $ meanFlowByArea_ByRiver                   : num [1:516898] NaN NaN 0.598 NaN 0.135 ...
 $ sdFlow                                   : num [1:516898] NA NA 0.58 NA 0.181 ...
 $ sdFlowByRiver                            : num [1:516898] NA NA 0.4467 NA 0.0767 ...
 $ sdFlowByArea_flowExt                     : num [1:516898] NA NA 0.58 NA 0.181 ...
 $ sdFlowByArea_ByRiver                     : num [1:516898] NA NA 0.4467 NA 0.0767 ...
 $ propBelowLoFlowThreshByRiver             : num [1:516898] NaN NaN 0 NaN 0 ...
 $ propAboveHiFlowThreshByRiver             : num [1:516898] NaN NaN 0.182 NaN 0 ...
 $ propBelowLoFlowThreshByArea_flowExt      : num [1:516898] NaN NaN 0.0682 NaN 0.0901 ...
 $ propAboveHiFlowThreshByArea_flowExt      : num [1:516898] NaN NaN 0.148 NaN 0.018 ...
 $ riverOrdered                             : Ord.factor w/ 4 levels "West Brook"<"WB Jimmy"<..: 4 1 1 1 1 1 1 4 4 1 ...
 $ readerId                                 : chr [1:516898] NA NA NA NA ...
 $ sectionN                                 : num [1:516898] 7 1 40 44 25 32 32 3 3 23 ...
 $ aliveOrDead                              : chr [1:516898] "alive" "alive" "alive" "alive" ...
 $ instance                                 : num [1:516898] NA NA NA NA NA NA NA NA NA NA ...
 $ quarter                                  : num [1:516898] NA NA NA NA NA NA NA NA NA NA ...
 $ leftOrRight                              : chr [1:516898] NA NA NA NA ...
 $ habitat                                  : chr [1:516898] NA NA NA NA ...
 $ cover                                    : chr [1:516898] NA NA NA NA ...
 $ justification                            : chr [1:516898] NA NA NA NA ...
 $ sectionWQuarter                          : num [1:516898] NA NA NA NA NA NA NA NA NA NA ...
 $ j                                        : num [1:516898] NA NA NA NA NA NA NA NA NA NA ...
 $ date                                     : Date[1:516898], format: "2013-03-25" "2012-06-07" ...
 $ dummy                                    : num [1:516898] 1 1 1 1 1 1 1 1 1 1 ...
 $ sectionOriginal                          : chr [1:516898] "7" "1" "40" "44" ...
 $ inside                                   : logi [1:516898] TRUE TRUE TRUE TRUE TRUE TRUE ...
 $ yday                                     : num [1:516898] 84 159 88 176 160 271 87 260 335 270 ...
 $ ageInSamples                             : num [1:516898] 3 0 3 4 4 5 7 1 2 1 ...
 $ isYOY                                    : logi [1:516898] TRUE TRUE TRUE FALSE FALSE FALSE ...
 $ lagSection                               : num [1:516898] NA NA 44 NA 32 32 NA 3 NA 24 ...
 $ distMoved                                : num [1:516898] NA NA -4 NA -7 0 NA 0 NA -1 ...
 $ lagObservedWeight                        : num [1:516898] NA NA 41.3 NA 18.3 30.4 NA 4.8 NA 12.7 ...
 $ lagObservedLength                        : num [1:516898] NA NA 154 NA 120 147 NA 74 NA 108 ...
 $ lagSampleNumber                          : num [1:516898] NA NA 74 NA 71 73 NA 80 NA 73 ...
 $ sampleNumberDiff                         : num [1:516898] NA NA 1 NA 1 2 NA 1 NA 2 ...
 $ grWeight                                 : num [1:516898] NA NA 0.0135 NA 0.0015 ...
 $ grLength                                 : num [1:516898] NA NA 0.5102 NA 0.0631 ...
 $ minSample                                : num [1:516898] 73 70 73 73 70 70 70 79 79 71 ...
 $ maxSample                                : num [1:516898] 73 70 74 74 73 73 73 80 80 74 ...
 $ minYear                                  : num [1:516898] 1997 1997 1997 1997 1997 ...
 $ moveDir                                  : num [1:516898] NA NA -1 NA -1 0 NA 0 NA -1 ...
 $ sampleInterval                           : num [1:516898] NA NA 88.2 NA 110.9 ...
 $ drainage                                 : chr [1:516898] "west" "west" "west" "west" ...
 $ quarter_length                           : num [1:516898] 5 5.2 5.03 4.83 4.75 ...
 $ river_meter                              : num [1:516898] 5252 4394 5208 5292 4903 ...
 $ confluence_river_meter                   : num [1:516898] 5114 0 0 0 0 ...
 $ riverMeter                               : num [1:516898] 5252 4394 5208 5292 4903 ...
 $ nPasses                                  : num [1:516898] 1 2 1 2 2 2 1 1 1 2 ...
 $ wGR_Slope                                : num [1:516898] -0.397 -0.535 -0.367 -0.535 -0.278 ...
 $ grWeightS                                : num [1:516898] NA NA 4.98e-08 NA 7.90e-09 ...
 $ lw_intercept                             : num [1:516898] -5.02 -5.02 -5.02 -5.02 -4.98 ...
 $ lw_slope                                 : num [1:516898] 3.02 3.02 3.02 3.02 3.01 ...
 $ cf                                       : num [1:516898] 0.0797 0.1133 0.0973 0.1131 0.1074 ...
 $ relCF                                    : num [1:516898] 0.0734 0.1043 0.0886 0.1022 0.102 ...
 $ nPerInd                                  : int [1:516898] 1 1 2 2 3 3 3 2 2 3 ...
 $ meanTemperatureScaledBySeason            : num [1:516898] NaN NaN 0.393 NaN 0.819 ...
 $ meanFlowScaledBySeason                   : num [1:516898] NaN NaN 0.526 NaN -0.736 ...
 $ meanFlowByRiverScaledBySeason            : num [1:516898] NaN NaN 0.881 NaN -0.225 ...
 $ meanFlowByArea_flowExtScaledBySeason     : num [1:516898] NaN NaN 0.822 NaN -0.608 ...
 $ meanFlowByArea_ByRiverScaledBySeason     : num [1:516898] NaN NaN 0.881 NaN -0.225 ...
 $ meanTemperatureScaledBySeasonRiver       : num [1:516898] NaN NaN 0.429 NaN 0.771 ...
 $ meanFlowScaledBySeasonRiver              : num [1:516898] NaN NaN 0.609 NaN -0.832 ...
 $ meanFlowByRiverScaledBySeasonRiver       : num [1:516898] NaN NaN 0.667 NaN -0.713 ...
 $ meanFlowByArea_flowExtScaledBySeasonRiver: num [1:516898] NaN NaN 0.609 NaN -0.832 ...
 $ meanFlowByArea_ByRiverScaledBySeasonRiver: num [1:516898] NaN NaN 0.667 NaN -0.713 ...

Facet by river, symbol identifies survey type

One panel, symbol identifies river

---
title: "All fish data"
format:
  html:
    toc: false
    echo: false
    keep-hidden: false
    code-tools: true
---

Note: need to do a full book render to get OJS functionality.

## Merge All data

quarto-executable-code-5450563D

```r
#| label: dataAllOptions
#| include: false
knitr::opts_chunk$set(message = FALSE, warning = FALSE)
```

quarto-executable-code-5450563D

```r
#| label: librariesdataAll
#| echo: false
#| cache: false

#library(devtools)
#options(download.file.method = "wininet") or turn off VPN
#devtools::install_github('Conte-Ecology/westBrookData/getWBData')
library(tidyverse)
library(lubridate)
library(validate)
library(targets)


# devtools::install_github('bletcher/getPrepareWBData')
# use this if command above doesn't work: options(download.file.method = "wininet")
#library(getPrepareWBData)
```

### Read merged data target
Variables in the `all data` dataset
quarto-executable-code-5450563D

```r
#| label: dataAll
#| cache: false

all <- tar_read(cdWB_all)
  
str(all)
#ojs_define(allOJS0 = all) can't use whole dataset, html file gets too big

# Save rData file for downloading
# save(all, file = "C:/Users/bletcher/OneDrive - DOI/projects/wbBook_quarto_targets/data/outForDownload/cdWB_all.RData")

```

quarto-executable-code-5450563D

```r
#| label: tagsAllDefineN
#| cache: false

tags <- all %>%
  group_by(tag) %>%
  summarize(n = n()) %>%
  #arrange(desc(n)) %>%
  filter(!is.na(tag))

all <- all %>% 
  left_join(tags)

allGT1500 <- all %>%
  filter(n > 500, n < 10000) |> 
  dplyr::select(tag, n, detectionDate, riverMeter, river, section, survey)


ojs_define(allOJS0 = allGT1500)
```

quarto-executable-code-5450563D

```r
# allTop10 <- all %>%
#   filter(tag %in% tags10$tag) %>%
#   select(tag, detectionDate, river, section, observedLength)
#   
#   
# ojs_define(allOJS = allTop10)
```

quarto-executable-code-5450563D

```ojs
Plot = import("https://esm.sh/@observablehq/plot")

allOJS = transpose(allOJS0)

tags = tidy(allOJS, groupBy("tag", [summarize({ n: n() })]), arrange([desc("n")]))
```

quarto-executable-code-5450563D

```ojs
//| panel: input

viewof selectTag = Inputs.select(
  tags.map((d) => d.tag),
  {
    //value: "1bf20ebcae",
    label: "Tags:",
    multiple: true
  }
)

```

Facet by river, symbol identifies survey type
quarto-executable-code-5450563D

```ojs
tagsToPlot0 = allOJS.filter((d) => selectTag.includes(d.tag))

tagsToPlot = tidy(
  tagsToPlot0,
  mutate({
    newDate: (d) => new Date(d.detectionDate)
  })
)

tagsOverTime = Plot.plot({
  marks: [
    Plot.dot(tagsToPlot, {
      x: "newDate",
      y: "riverMeter",
      stroke: "tag",
      symbol: "survey"
    }),
    Plot.line(tagsToPlot, {
      x: "newDate",
      y: "riverMeter",
      stroke: "tag"
    }),
    Plot.axisX({ ticks: "3 months" })
  ],
  
  facet: {
    data: tagsToPlot, x: "river"
  },
  // Include a legend for the color channel 
  color: {
    legend: true,
  },
  symbol: {
    legend: true
  }
})
```

One panel, symbol identifies river
quarto-executable-code-5450563D

```ojs
tagsOverTimeRiver = Plot.plot({
  marks: [
    Plot.dot(tagsToPlot, {
      x: "newDate",
      y: "riverMeter",
      stroke: "tag",
      symbol: "river"
    }),
    Plot.line(tagsToPlot, { x: "newDate", y: "riverMeter", stroke: "tag" }),
    Plot.axisX({ ticks: "3 months" })
  ],
  // Include a legend for the color channel
  color: {
    legend: true
  },
  symbol: {
    legend: true
  }
})
```


quarto-executable-code-5450563D

```ojs
selectTag
tagsToPlot
allOJS
```


quarto-executable-code-5450563D

```ojs
import {tidy, max, nDistinct, groupBy, mutate, filter, summarize, first, last, distinct, count, n, arrange, mutateWithSummary, desc, debug, sum, slice, mean, deviation, variance, meanRate, median, min, total, sliceMax, innerJoin, leftJoin, pivotLonger, pivotWider, select, rename, complete, contains, when} from "@pbeshai/tidyjs"

```